package com.phpxiu.app.model.msg;
import com.phpxiu.app.kkylive.KKYApp;
import com.phpxiu.app.kkylive.R;
import com.phpxiu.app.model.list.ListItem;
import com.tencent.TIMMessage;
import com.tencent.bugly.imsdk.crashreport.CrashReport;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 *私信消息数据基类
 * Created by 吴金洲 on 2016/7/26
 */
public abstract class C2CMsg extends ListItem {
    protected final String TAG = "C2CMsg";
    protected TIMMessage message;
    protected boolean isShowTime=false;
    public TIMMessage getMessage() {
        return message;
    }

    /**
     * 判断是否是自己发的
     */
    public boolean isSelf(){
        return message.isSelf();
    }

    /**
     * 删除消息
     */
    public void remove(){
        if (message != null){
            message.remove();
        }
    }

    /**
     * 是否需要显示时间
     */
    public boolean isShowTime() {
       return isShowTime;
    }

    public void setShowTime(TIMMessage message){
        if (message == null){
            isShowTime=true;
            return;
        }
        isShowTime=this.message.timestamp() - message.timestamp() > 300;
    }
    /**
     * 时间转化为显示字符串
     *
     * @param timeStamp 单位为秒
     */
    public static String getTimeStr(long timeStamp){
        if (timeStamp==0) return "";
        Calendar inputTime = Calendar.getInstance();
        inputTime.setTimeInMillis(timeStamp*1000);
        Date currenTimeZone = inputTime.getTime();
        Calendar calendar = Calendar.getInstance();
        if (calendar.before(inputTime)){
            //当前时间在输入时间之前
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            return sdf.format(currenTimeZone);
        }
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        if (calendar.before(inputTime)){
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
            return sdf.format(currenTimeZone);
        }
        calendar.add(Calendar.DAY_OF_MONTH,-1);
        if (calendar.before(inputTime)){
            return KKYApp.getInstance().getResources().getString(R.string.ui_yesterday);
        }else{
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            calendar.set(Calendar.MONTH, Calendar.JANUARY);
            if (calendar.before(inputTime)){
                SimpleDateFormat sdf = new SimpleDateFormat("M-d");
                return sdf.format(currenTimeZone);
            }else{
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                return sdf.format(currenTimeZone);

            }

        }

    }

    /**
     * 时间转化为聊天界面显示字符串
     *
     * @param timeStamp 单位为秒
     */
    public static String getChatTimeStr(long timeStamp){
        try{
            if (timeStamp==0) return "";
            Calendar inputTime = Calendar.getInstance();
            inputTime.setTimeInMillis(timeStamp*1000);
            Date currenTimeZone = inputTime.getTime();
            Calendar calendar = Calendar.getInstance();
            if (calendar.before(inputTime)){
                //当前时间在输入时间之前
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                return sdf.format(currenTimeZone);
            }
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            if (calendar.before(inputTime)){
                SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
                return sdf.format(currenTimeZone);
            }
            calendar.add(Calendar.DAY_OF_MONTH,-1);
            if (calendar.before(inputTime)){
                SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
                return KKYApp.getInstance().getResources().getString(R.string.ui_yesterday)+" "+sdf.format(currenTimeZone);
            }else{
                calendar.set(Calendar.DAY_OF_MONTH, 1);
                calendar.set(Calendar.MONTH, Calendar.JANUARY);
                if (calendar.before(inputTime)){
                    SimpleDateFormat sdf = new SimpleDateFormat("M-d HH:mm");
                    return sdf.format(currenTimeZone);
                }else{
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                    return sdf.format(currenTimeZone);
                }

            }
        }catch (IllegalArgumentException e){
            CrashReport.postCatchedException(e);
        }
        return "";
    }
}
